package weaver.jobs.baicheng;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



/***
 * 柏城-考勤同步--考勤地点记录表
 * @author lingman
 * 2018-07-12
 * modify date:2018-09-03
 */
public class kaoqin_newSyn extends BaseCronJob {
	
	//private static double EARTH_RADIUS = 6378.137; 
	private static final double EARTH_RADIUS = 6378137;// 地球半径
    
    private static double rad(double d) { 
        return d * Math.PI / 180.0; 
    }
	
	
	@Override
	public void execute() {
		
		Calendar calendar = Calendar.getInstance();  
        calendar.setTime(new Date());  
        calendar.add(Calendar.DAY_OF_MONTH, -1);  
        String exday=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
//        System.out.println(exday);
        
		writeLog(exday+",开始执行柏城-考勤同步...");
		
		RecordSet rs = new RecordSet();
		RecordSet recordSet_kq = new RecordSet();
		RecordSet recordSet_kqin = new RecordSet();
		writeLog(exday+",开始执行柏城-考勤同步,test001");
		String sql_hrmsign="select id,signAddr,LONGITUDE,LATITUDE from hrmschedulesign where isInCom=1 and signDate in('2023-01-06','2023-01-07','2023-01-08')";
//		String sql_hrmsign="select id,signAddr,LONGITUDE,LATITUDE from hrmschedulesign where isInCom=1 and signDate='"+exday+"'";
//		String sql_hrmsign="select id,signAddr,LONGITUDE,LATITUDE from hrmschedulesign where signDate='"+"2018-07-13"+"'";
		
		writeLog(exday+",开始执行柏城-考勤同步,sql_hrmsign:"+sql_hrmsign);
		rs.executeSql(sql_hrmsign);
		while(rs.next()){
			String id = rs.getString("id");   //签到表主键id
			String signAddr = rs.getString("signAddr");   //考勤地点
			String LONGITUDE = rs.getString("LONGITUDE");   //经度
			String LATITUDE = rs.getString("LATITUDE");   //维度
			
			writeLog(exday+",开始执行柏城-考勤同步,id:"+id+",signAddr:"+signAddr+",LONGITUDE:"+LONGITUDE+",LATITUDE:"+LATITUDE);
			
			if(!"".equals(signAddr)){
				String sql_kqdd="select bz from uf_rykqdd where id="+signAddr;
				writeLog(exday+",开始执行柏城-考勤同步,id:"+id+",sql_kqdd:"+sql_kqdd);
				recordSet_kq.executeSql(sql_kqdd);
				String bz = "";
				if(recordSet_kq.next()){
					bz = recordSet_kq.getString("bz");   //卡机编号
					
				}
				
				String sql_ins="insert into uf_kqdd(kqbh,kqdd,kjbh) values('"+id+"','"+signAddr+"','"+bz+"')";
				writeLog(exday+",开始执行柏城-考勤同步,id:"+id+",sql_ins:"+sql_ins);
				recordSet_kqin.executeSql(sql_ins);
			}else{
				writeLog(exday+",开始执行柏城-考勤同步,id:"+id+",signAddr为空");
				//计算经纬度之间的差值
				String kqdd=getKqdd(id,LONGITUDE,LATITUDE);
				if("".equals(kqdd)){
					writeLog(exday+",开始执行柏城-考勤同步,id:"+id+",未查到符合条件的考勤地点");
					
				}else{
					String arr[]=kqdd.split(",");
					String sql_ins="insert into uf_kqdd(kqbh,kqdd,kjbh) values('"+id+"','"+arr[0]+"','"+arr[1]+"')";
					writeLog(exday+",开始执行柏城-考勤同步,id:"+id+",sql_ins1:"+sql_ins);
					recordSet_kqin.executeSql(sql_ins);
					
				}
				
			}
			
		}
		
		writeLog(exday+",完成柏城-考勤同步...");

		//super.execute();
	}
	
	
	public void writeLog(Object obj) {
		if(true){
			new BaseBean().writeLog(this.getClass().getName(), obj);
		}
	}
	
    
	
    /**
     * 根据两个位置的经纬度，来计算两地的距离（单位为KM）
     * 参数为String类型

     * @return
     */
    public double getDistance(String lat1Str, String lng1Str, String lat2Str, String lng2Str) {
        Double lat1 = Double.parseDouble(lat1Str);
        Double lng1 = Double.parseDouble(lng1Str);
        Double lat2 = Double.parseDouble(lat2Str);
        Double lng2 = Double.parseDouble(lng2Str);
        
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double difference = radLat1 - radLat2;
        double mdifference = rad(lng1) - rad(lng2);
        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(difference / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(mdifference / 2), 2)));
        distance = distance * EARTH_RADIUS;
        //保留4位小数
        DecimalFormat dec = new DecimalFormat("0.0000");
        String s= dec.format(distance);
//        distance = Math.round(distance * 10000) / 10000;
//        String distanceStr = s+"";
//        distanceStr = distanceStr.substring(0, distanceStr.indexOf("."));
        
        return Double.parseDouble(s);
    }
	
    
    
    /**
      * 计算两个经度纬度(WGS-84)之间的距离
      * @param lng1
      *            坐标1经度
      * @param lat1
      *            坐标1纬度
      * @param lng2
      *            坐标2经度
      * @param lat2
      *            坐标2纬度
      * @return 2个坐标之间距离,单位:米
     */
    
    private double getdistance(double lng1,double lat1,double lng2,double lat2){
    	double radLat1 = Math.toRadians(lat1);
    	double radLat2 = Math.toRadians(lat2);
    	double a=Math.abs(radLat1-radLat2);
    	double b =Math.abs(Math.toRadians(lng1) - Math.toRadians(lng2));
    	double distance = 2*Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    	distance =distance*EARTH_RADIUS;
    	distance =Math.round(distance*10000)/10000;
    	return distance;
    }
    
    
    
    
    
    //根据经纬度判断是属于哪个考勤点
    public String getKqdd(String signid,String LONGITUDE, String LATITUDE) {
    	RecordSet rs = new RecordSet();
    	rs.writeLog("开始执行柏城-考勤同步,signid:"+signid+",进入经纬度计算逻辑");
    	String sql="select id,jd,wd,bz from uf_rykqdd";
    	rs.writeLog("开始执行柏城-考勤同步,signid:"+signid+",进入经纬度计算逻辑,sql:"+sql);
    	rs.executeSql(sql);
    	double distance=0;
		while(rs.next()){
			String id = rs.getString("id");   //
			String jd = rs.getString("jd");   //经度
			String wd = rs.getString("wd");   //纬度
			String bz = rs.getString("bz");   //卡机编号
			rs.writeLog("开始执行柏城-考勤同步,signid:"+signid+",进入经纬度计算逻辑,id:"+id+",jd:"+jd+",wd:"+wd+",bz:"+bz+",LONGITUDE:"+LONGITUDE+",LATITUDE:"+LATITUDE);
			try{
				//distance=getDistance(jd,wd,LONGITUDE,LATITUDE);
				distance=getdistance(Double.parseDouble(jd),Double.parseDouble(wd),Double.parseDouble(LONGITUDE),Double.parseDouble(LATITUDE));
			}catch (Exception e) {
				// TODO: handle exception
				rs.writeLog("开始执行柏城-考勤同步,signid:"+signid+",进入经纬度计算逻辑,抛出异常");
				continue;
			}
			
			rs.writeLog("开始执行柏城-考勤同步,signid:"+signid+",进入经纬度计算逻辑,id:"+id+",jd:"+jd+",wd:"+wd+",bz:"+bz+",distance:"+distance);
			
			//if(distance<=3){
			if(distance<=1000){
				rs.writeLog("开始执行柏城-考勤同步,signid:"+signid+",进入经纬度计算逻辑,id:"+id+",找到正确结果:"+id+","+bz);
				return id+","+bz;
			}
		}
    	
    	return "";
    }
    
}
